{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "src_text = \"\"\"A\t．━\n",
    "B\t━ ．．．\n",
    "C\t━ ．━ ．\n",
    "D\t━ ．.\n",
    "E\t．\n",
    "F\t．．━ ．\n",
    "G\t━ ━ ．\n",
    "H\t．．．．\n",
    "I\t．．\n",
    "J\t．━ ━ ━\n",
    "K\t━ ．━\n",
    "L\t．━ ．．\n",
    "M\t━ ━\n",
    "N\t━ ．\n",
    "O\t━ ━ ━\n",
    "P\t．━ ━ ．\n",
    "Q\t━ ━ ．━\n",
    "R\t．━ ．\n",
    "S\t．．．\n",
    "T\t━\n",
    "U\t．．━\n",
    "V\t．．．━\n",
    "W\t．━ ━\n",
    "X\t━ ．．━\n",
    "Y\t━ ．━ ━\n",
    "Z\t━ ━ ．．\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "26"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "moorse_table_dict_ = dict()\n",
    "for line in src_text.split('\\n'):\n",
    "    k, v = line.split('\\t')\n",
    "    moorse_table_dict_[k] = v\n",
    "moorse_table_dict_\n",
    "len(moorse_table_dict_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "A ．━\nB ━ ．．．\nC ━ ．━ ．\nD ━ ．.\nE ．\nF ．．━ ．\nG ━ ━ ．\nH ．．．．\nI ．．\nJ ．━ ━ ━\nK ━ ．━\nL ．━ ．．\nM ━ ━\nN ━ ．\nO ━ ━ ━\nP ．━ ━ ．\nQ ━ ━ ．━\nR ．━ ．\nS ．．．\nT ━\nU ．．━\nV ．．．━\nW ．━ ━\nX ━ ．．━\nY ━ ．━ ━\nZ ━ ━ ．．\n"
    }
   ],
   "source": [
    "tmp = src_text.split('\\n')\n",
    "for line in tmp:\n",
    "    k, v = line.split('\\t')\n",
    "    print(k, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'mtb.csv'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-7f1500877b31>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcsv\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mmtb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'mtb.csv'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'r'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'UTF-8'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mrd\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcsv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mrow\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrd\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'mtb.csv'"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "mtb = dict()\n",
    "with open('mtb.csv', 'r', encoding='UTF-8') as f:\n",
    "    rd = csv.reader(f)\n",
    "    for row in rd:\n",
    "        print(row[0][-1], row[1])\n",
    "        mtb[row[0][-1]] = row[1]     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "{}\n"
    }
   ],
   "source": [
    "print(mtb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "mtb = {\n",
    "    'A': '·-',\n",
    "    'B': '-···',\n",
    "    'C': '-·-·',\n",
    "    'D': '-··',\n",
    "    'E': '·',\n",
    "    'F': '··-·',\n",
    "    'G': '--·',\n",
    "    'H': '····',\n",
    "    'I': '··',\n",
    "    'J': '·---',\n",
    "    'K': '-·-',\n",
    "    'L': '·-··',\n",
    "    'M': '--',\n",
    "    'N': '-·',\n",
    "    'O': '---',\n",
    "    'P': '·--·',\n",
    "    'Q': '--·-',\n",
    "    'R': '·-·',\n",
    "    'S': '···',\n",
    "    'T': '-',\n",
    "    'U': '··-',\n",
    "    'V': '···-',\n",
    "    'W': '·--',\n",
    "    'X': '-··-',\n",
    "    'Y': '-·--',\n",
    "    'Z': '--··',\n",
    "    '1': '·----',\n",
    "    '2': '··---',\n",
    "    '3': '···--',\n",
    "    '4': '····',\n",
    "    '5': '·····',\n",
    "    '6': '-····',\n",
    "    '7': '--···',\n",
    "    '8': '---··',\n",
    "    '9': '----·',\n",
    "    '0': '-----',\n",
    "    '.': '·-·-·-',\n",
    "    ':': '---···',\n",
    "    ',': '--··--',\n",
    "    ';': '-·-·-·',\n",
    "    '?': '··--··',\n",
    "    '=': '-···-',\n",
    "    '\\'': '·----·',\n",
    "    '/': '-··-·',\n",
    "    '!': '-·-·--',\n",
    "    '-': '-····-',\n",
    "    '_': '··--·-',\n",
    "    '\"': '·-··-·',\n",
    "    '(': '-·--·',\n",
    "    ')': '-·--·-',\n",
    "    '$': '···-··-',\n",
    "    '&': '·-···',\n",
    "    '@': '·--·-·',\n",
    "    '+': '·-·-·',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "src_text = \"\"\"基础拉丁字母\n",
    "字符\t代码\t字符\t代码\t字符\t代码\t字符\t代码\t字符\t代码\t字符\t代码\t字符\t代码\n",
    "A\t·-\tB\t-···\tC\t-·-·\tD\t-··\tE\t·\tF\t··-·\tG\t--·\n",
    "H\t····\tI\t··\tJ\t·---\tK\t-·-\tL\t·-··\tM\t--\tN\t-·\n",
    "O\t---\tP\t·--·\tQ\t--·-\tR\t·-·\tS\t···\tT\t-\tU\t··-\n",
    "V\t···-\tW\t·--\tX\t-··-\tY\t-·--\tZ\t--··\t\t\t\t\n",
    "数字\n",
    "摩尔斯电码的数字有长码和短码，通常用长码，于中文电码中组成汉字则使用短码。\n",
    "\n",
    "字符\t长码\t短码\t字符\t长码\t短码\t字符\t长码\t短码\t字符\t长码\t短码\t字符\t长码\t短码\n",
    "1\t·----\t·-\t2\t··---\t··-\t3\t···--\t···-\t4\t····-\t····-\t5\t·····\t·····\n",
    "6\t-····\t-····\t7\t--···\t-···\t8\t---··\t-··\t9\t----·\t-·\t0\t-----\t-\n",
    "标点符号\n",
    "字符\t代码\t字符\t代码\t字符\t代码\t字符\t代码\t字符\t代码\t字符\t代码\n",
    ".\t·-·-·-\t:\t---···\t,\t--··--\t;\t-·-·-·\t?\t··--··\t=\t-···-\n",
    "'\t·----·\t/\t-··-·\t!\t-·-·--\t-\t-····-\t_\t··--·-\t\"\t·-··-·\n",
    "(\t-·--·\t)\t-·--·-\t$\t···-··-\t&\t·-···\t@\t·--·-·\t+\t·-·-·\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "A ·-\nB -···\nC -·-·\nD -··\nE ·\nF ··-·\nG --·\nH ····\nI ··\nJ ·---\nK -·-\nL ·-··\nM --\nN -·\nO ---\nP ·--·\nQ --·-\nR ·-·\nS ···\nT -\nU ··-\nV ···-\nW ·--\nX -··-\nY -·--\nZ --··\n1 ·----\n2 ··---\n3 ···--\n4 ····-\n5 ·····\n6 -····\n7 --···\n8 ---··\n9 ----·\n0 -----\n. ·-·-·-\n: ---···\n, --··--\n; -·-·-·\n? ··--··\n= -···-\n' ·----·\n/ -··-·\n! -·-·--\n- -····-\n_ ··--·-\n\" ·-··-·\n( -·--·\n) -·--·-\n$ ···-··-\n& ·-···\n@ ·--·-·\n+ ·-·-·\n"
    }
   ],
   "source": [
    "rmtb = dict()\n",
    "for k, v in re.findall('(.)\\t([-·]+)', src_text):\n",
    "    print(k, v)\n",
    "    mtb[k] = v\n",
    "    rmtb[v] = k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'·-': 'A',\n '-···': 'B',\n '-·-·': 'C',\n '-··': 'D',\n '·': 'E',\n '··-·': 'F',\n '--·': 'G',\n '····': 'H',\n '··': 'I',\n '·---': 'J',\n '-·-': 'K',\n '·-··': 'L',\n '--': 'M',\n '-·': 'N',\n '---': 'O',\n '·--·': 'P',\n '--·-': 'Q',\n '·-·': 'R',\n '···': 'S',\n '-': 'T',\n '··-': 'U',\n '···-': 'V',\n '·--': 'W',\n '-··-': 'X',\n '-·--': 'Y',\n '--··': 'Z',\n '·----': '1',\n '··---': '2',\n '···--': '3',\n '····-': '4',\n '·····': '5',\n '-····': '6',\n '--···': '7',\n '---··': '8',\n '----·': '9',\n '-----': '0',\n '·-·-·-': '.',\n '---···': ':',\n '--··--': ',',\n '-·-·-·': ';',\n '··--··': '?',\n '-···-': '=',\n '·----·': \"'\",\n '-··-·': '/',\n '-·-·--': '!',\n '-····-': '-',\n '··--·-': '_',\n '·-··-·': '\"',\n '-·--·': '(',\n '-·--·-': ')',\n '···-··-': '$',\n '·-···': '&',\n '·--·-·': '@',\n '·-·-·': '+'}"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "rmtb"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}